<!DOCTYPE html><html lang="[&quot;zh-Hans&quot;,&quot;zh-CN&quot;,&quot;default&quot;]"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"><meta name="description" content="react+百度地图实现自定义图标"><meta name="keywords" content="react"><meta name="author" content="wenmu"><meta name="copyright" content="wenmu"><title>react+百度地图实现自定义图标 | 温木的博客</title><link rel="shortcut icon" href="/melody-favicon.ico"><link rel="stylesheet" href="/css/index.css?version=1.6.1"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome@latest/css/font-awesome.min.css?version=1.6.1"><link rel="dns-prefetch" href="https://cdn.staticfile.org"><link rel="dns-prefetch" href="https://cdn.bootcss.com"><link rel="dns-prefetch" href="https://creativecommons.org"><link rel="dns-prefetch" href="http://ta.qq.com"><script>(function() {
   var hm = document.createElement("script");
   hm.src = "https://tajs.qq.com/stats?sId=&lt;script type=&quot;text/javascript&quot; src=&quot;http://tajs.qq.com/stats?sId=66523541&quot; charset=&quot;UTF-8&quot;&gt;&lt;/script&gt;";
   var s = document.getElementsByTagName("script")[0];
   s.parentNode.insertBefore(hm, s);
 })();</script><script>var GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  }
} </script><meta name="generator" content="Hexo 4.2.0"></head><body><i class="fa fa-arrow-right" id="toggle-sidebar" aria-hidden="true"></i><div id="sidebar"><div class="toggle-sidebar-info text-center"><span data-toggle="切换文章详情">切换站点概览</span><hr></div><div class="sidebar-toc"><div class="sidebar-toc__title">目录</div><div class="sidebar-toc__progress"><span class="progress-notice">你已经读了</span><span class="progress-num">0</span><span class="progress-percentage">%</span><div class="sidebar-toc__progress-bar"></div></div><div class="sidebar-toc__content"><ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#开发环境"><span class="toc-number">1.</span> <span class="toc-text">开发环境</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#实现效果"><span class="toc-number">2.</span> <span class="toc-text">实现效果</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#难点"><span class="toc-number">3.</span> <span class="toc-text">难点</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#思路"><span class="toc-number">4.</span> <span class="toc-text">思路</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#解决方法"><span class="toc-number">5.</span> <span class="toc-text">解决方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#外框的实现"><span class="toc-number">6.</span> <span class="toc-text">外框的实现</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#相关-code"><span class="toc-number">7.</span> <span class="toc-text">相关 code</span></a></li></ol></div></div><div class="author-info hide"><div class="author-info__avatar text-center"><img src="https://i.loli.net/2020/02/27/xwaBoOj158MfNyq.jpg"></div><div class="author-info__name text-center">wenmu</div><div class="author-info__description text-center">记录一些在工作中或学习新知识时学到的一些知识点；正所谓好记性不如一个烂笔头，温故而知新！</div><hr><div class="author-info-articles"><a class="author-info-articles__archives article-meta" href="/archives"><span class="pull-left">文章</span><span class="pull-right">115</span></a><a class="author-info-articles__tags article-meta" href="/tags"><span class="pull-left">标签</span><span class="pull-right">52</span></a><a class="author-info-articles__categories article-meta" href="/categories"><span class="pull-left">分类</span><span class="pull-right">30</span></a></div></div></div><div id="content-outer"><div class="no-bg" id="top-container"><div id="page-header"><span class="pull-left"> <a id="site-name" href="/">温木的博客</a></span><i class="fa fa-bars toggle-menu pull-right" aria-hidden="true"></i><span class="pull-right menus"><a class="site-page" href="/">首页</a><a class="site-page" href="/archives">文章</a><a class="site-page" href="/tags">标签</a><a class="site-page" href="/categories">分类</a></span></div><div id="post-info"><div id="post-title">react+百度地图实现自定义图标</div><div id="post-meta"><time class="post-meta__date"><i class="fa fa-calendar" aria-hidden="true"></i> 2020-02-28</time><span class="post-meta__separator">|</span><i class="fa fa-inbox post-meta__icon" aria-hidden="true"></i><a class="post-meta__categories" href="/categories/react/">react</a></div></div></div><div class="layout" id="content-inner"><article id="post"><div class="article-container" id="post-content"><h3 id="开发环境"><a href="#开发环境" class="headerlink" title="开发环境"></a>开发环境</h3><ol>
<li>react</li>
<li>百度地图使用的是：rc-bmap</li>
</ol>
<h3 id="实现效果"><a href="#实现效果" class="headerlink" title="实现效果"></a>实现效果</h3><p><img src="https://i.loli.net/2020/02/28/hPBmSnrD2d65eHO.jpg" alt="图片描述"></p>
<p>实现图中提示框的效果。<br>看到这个，用过百度地图的第一个会想到 marker，其实不是，用的是 Label，其实难点不在这，这个看看 API，都能很快琢磨出来。</p>
<h3 id="难点"><a href="#难点" class="headerlink" title="难点"></a>难点</h3><ol>
<li>如何显示成带图标的那个外框</li>
<li>里面的内容如何换行，及设置样式</li>
</ol>
<h3 id="思路"><a href="#思路" class="headerlink" title="思路"></a>思路</h3><p>看了 API,<code>Marker</code>不能显示内容,但<code>Marker</code>可以使用<code>Label</code>属性，其实也是用的<code>Label</code>；<br>所以开始研究 Label，首先<code>Label</code>是显示内容的，但是<code>label</code>的<code>content</code>属性只能是<code>string</code>,这就麻烦了，怎么控制字体显示呢，怎么设置样式呢？<br>原来是使用了 jsx 的语法来写，显示的是<code>[Object Object]</code>，显然不支持啊。</p>
<h3 id="解决方法"><a href="#解决方法" class="headerlink" title="解决方法"></a>解决方法</h3><p>最后根据经验试了下 html 字符串，字符串上套了层 div，原以为 div 会显示出来，结果没有。</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">&lt;Label</span><br><span class="line">  content=&#123;<span class="string">`&lt;div&gt;骑手正在取件途中&lt;/div&gt;&lt;div&gt;1.8公里&lt;/div&gt;`</span>&#125;</span><br><span class="line">  point=&#123;point&#125;</span><br><span class="line">  offset=&#123;courierOffset&#125;</span><br><span class="line">  massClear=&#123;<span class="literal">false</span>&#125;</span><br><span class="line">  zIndex=&#123;<span class="number">100</span>&#125; <span class="comment">// 同html的z-index</span></span><br><span class="line">  style=&#123;labelStyle&#125;</span><br><span class="line">/&gt;</span><br></pre></td></tr></table></figure>

<p>于是就知道这个是支持<code>hmtl字符串</code>的；<br>这样换行控制是解决了，接下来是样式,按照 jsx 的语法写，没效果，想着也合理。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">content=&#123;`&lt;div&gt;骑手正在取件途中&lt;/div&gt;&lt;div className=&#123;<span class="variable">$&#123;styles.label&#125;</span>&#125;&gt;1.8公里&lt;/div&gt;`&#125;</span><br></pre></td></tr></table></figure>

<p>于是又试了直接写 class</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">content=&#123;`&lt;div&gt;骑手正在取件途中&lt;/div&gt;&lt;div class=<span class="string">'label'</span>&gt;1.8公里&lt;/div&gt;`&#125;</span><br></pre></td></tr></table></figure>

<p>这样感觉也不合理，因为样式需要 babel 编译的，直接这样写，是识别不了的。<br>因此就有了<strong>解决方案</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">content=&#123;`&lt;div&gt;骑手正在取件途中&lt;/div&gt;&lt;div class=<span class="string">'$&#123;styles.label&#125;'</span>&gt;1.8公里&lt;/div&gt;`&#125;</span><br></pre></td></tr></table></figure>

<h3 id="外框的实现"><a href="#外框的实现" class="headerlink" title="外框的实现"></a>外框的实现</h3><p>外框其实通过设置 Label 的 style 来实现。</p>
<h3 id="相关-code"><a href="#相关-code" class="headerlink" title="相关 code"></a>相关 code</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> courierLabelBG <span class="keyword">from</span> <span class="string">"./images/courier_label_bg.svg"</span>; <span class="comment">//图片中带有前面的小车</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> point = &#123;</span><br><span class="line">  lng: <span class="number">116.404</span>,</span><br><span class="line">  lat: <span class="number">39.915</span>,</span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">const</span> &#123; Description &#125; = DescriptionList;</span><br><span class="line"><span class="keyword">const</span> ButtonGroup = Button.Group;</span><br><span class="line"><span class="keyword">const</span> labelStyle = &#123;</span><br><span class="line">  backgroundImage: <span class="string">`url(<span class="subst">$&#123;courierLabelBG&#125;</span>)`</span>,</span><br><span class="line">  backgroundRepeat: <span class="string">"no-repeat"</span>,</span><br><span class="line">  backgroundSize: <span class="string">"cover"</span>,</span><br><span class="line">  backgroundColor: <span class="string">"rgba(0, 0, 0, 0)"</span>,</span><br><span class="line">  border: <span class="string">"none"</span>,</span><br><span class="line">  color: <span class="string">"rgba(51,51,51,1);"</span>,</span><br><span class="line">  paddingLeft: <span class="string">"54px"</span>,</span><br><span class="line">  paddingTop: <span class="string">"8px"</span>,</span><br><span class="line">  fontWeight: <span class="string">"400"</span>,</span><br><span class="line">  width: <span class="string">"181px"</span>,</span><br><span class="line">  height: <span class="string">"52px"</span>,</span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">const</span> courierOffset = &#123;</span><br><span class="line">  width: <span class="number">-92</span>,</span><br><span class="line">  height: <span class="number">-54</span>,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">&lt;<span class="built_in">Map</span> ak=<span class="string">"WAeVpuoSBH4NswS30GNbCRrlsmdGB5Gv"</span> scrollWheelZoom&gt;</span><br><span class="line">  &lt;Label</span><br><span class="line">    content=&#123;<span class="string">`&lt;div&gt;骑手正在取件途中&lt;/div&gt;&lt;div class='<span class="subst">$&#123;styles.courierLabel&#125;</span>'&gt;1.8公里&lt;/div&gt;`</span>&#125;</span><br><span class="line">    point=&#123;point&#125;</span><br><span class="line">    offset=&#123;courierOffset&#125;</span><br><span class="line">    massClear=&#123;<span class="literal">false</span>&#125;</span><br><span class="line">    zIndex=&#123;<span class="number">100</span>&#125; <span class="comment">// 同html的z-index</span></span><br><span class="line">    style=&#123;labelStyle&#125;</span><br><span class="line">  /&gt;</span><br><span class="line">&lt;<span class="regexp">/Map&gt;;</span></span><br></pre></td></tr></table></figure>
</div></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">wenmu</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://blog.wangpengpeng.site/2020/02/28/react-%E7%99%BE%E5%BA%A6%E5%9C%B0%E5%9B%BE%E5%AE%9E%E7%8E%B0%E8%87%AA%E5%AE%9A%E4%B9%89%E5%9B%BE%E6%A0%87/">http://blog.wangpengpeng.site/2020/02/28/react-%E7%99%BE%E5%BA%A6%E5%9C%B0%E5%9B%BE%E5%AE%9E%E7%8E%B0%E8%87%AA%E5%AE%9A%E4%B9%89%E5%9B%BE%E6%A0%87/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank" rel="noopener">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://blog.wangpengpeng.site">温木的博客</a>！</span></div></div><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/react/">react</a></div><div class="post-qr-code"><div class="post-qr-code-item"><img class="post-qr-code__img" src="https://i.loli.net/2020/02/27/hOBzIxFDJMCrmtN.jpg"><div class="post-qr-code__desc">微信打赏</div></div></div><nav id="pagination"><div class="prev-post pull-left"><a href="/2020/02/28/%E5%A6%82%E4%BD%95%E8%BF%99%E6%B2%A1%E6%9C%89%E6%98%BE%E7%A4%BA%E5%99%A8%E7%9A%84%E6%83%85%E5%86%B5%E4%B8%8B%E8%8E%B7%E5%8F%96%E6%A0%91%E8%8E%93%E6%B4%BEIP%EF%BC%9F/"><i class="fa fa-chevron-left">  </i><span>如何这没有显示器的情况下获取树莓派IP？</span></a></div><div class="next-post pull-right"><a href="/2020/02/28/%E6%A0%91%E8%8E%93%E6%B4%BE3B-%E9%85%8D%E7%BD%AE%E5%9B%BD%E5%86%85%E6%BA%90%E5%92%8C%E5%AE%89%E8%A3%85docker/"><span>树莓派3B+配置国内源和安装docker</span><i class="fa fa-chevron-right"></i></a></div></nav><div id="vcomment"></div><script src="https://cdn1.lncld.net/static/js/3.0.4/av-min.js"></script><script src="https://cdn.jsdelivr.net/npm/valine/dist/Valine.min.js"></script><script>var notify = 'false' == true ? true : false;
var verify = 'false' == true ? true : false;
var GUEST_INFO = ['nick','mail','link'];
var guest_info = 'nick,mail,link'.split(',').filter(function(item){
  return GUEST_INFO.indexOf(item) > -1
});
guest_info = guest_info.length == 0 ? GUEST_INFO :guest_info;
window.valine = new Valine({
  el:'#vcomment',
  notify:notify,
  verify:verify,
  appId:'U1QqpeQNuBnRsfygHk1GQ30q-gzGzoHsz',
  appKey:'dunDQzEseDzgsKgw4qLQcog1',
  placeholder:'支持markdown！',
  avatar:'wavatar',
  guest_info:guest_info,
  pageSize:'20',
  lang: 'zh-cn'
})</script></div></div><footer><div class="layout" id="footer"><div class="copyright">&copy;2019 - 2020 By wenmu</div><div class="framework-info"><span>驱动 - </span><a href="http://hexo.io" target="_blank" rel="noopener"><span>Hexo</span></a><span class="footer-separator">|</span><span>主题 - </span><a href="https://github.com/Molunerfinn/hexo-theme-melody" target="_blank" rel="noopener"><span>Melody</span></a></div><div class="busuanzi"><script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><span id="busuanzi_container_page_pv">该文访问量<span id="busuanzi_value_page_pv"></span><span></span></span></div></div></footer><i class="fa fa-arrow-up" id="go-up" aria-hidden="true"></i><script src="https://cdn.jsdelivr.net/npm/animejs@latest/anime.min.js"></script><script src="https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@latest/velocity.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-ui-pack@latest/velocity.ui.min.js"></script><script src="/js/utils.js?version=1.6.1"></script><script src="/js/fancybox.js?version=1.6.1"></script><script src="/js/sidebar.js?version=1.6.1"></script><script src="/js/copy.js?version=1.6.1"></script><script src="/js/fireworks.js?version=1.6.1"></script><script src="/js/transition.js?version=1.6.1"></script><script src="/js/scroll.js?version=1.6.1"></script><script src="/js/head.js?version=1.6.1"></script><script>if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
  $('#nav').addClass('is-mobile')
  $('footer').addClass('is-mobile')
}</script></body></html>